package com.ttl.bc.ic.stktransfer2;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class BCStkTransfer2Action extends DispatchAction {

//	@SuppressWarnings("unchecked")
	//@Override
/*	protected Map getKeyMethodMap() {
		Map map = new HashMap();
		map.put("BCStkTransfer2.add", "add");
		map.put("BCStkTransfer2.update", "update");
		map.put("BCStkTransfer2.delete", "delete");
		return map;
	}*/

	public ActionForward blank(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		String dept = request.getParameter("dept");

		new BCStkTransfer2BO().setShelfToJSP(form, request);
//		String docNo = CommonUtils.getMaxDocNoBCStkTransfer2();
//		request.setAttribute("docNo", docNo);		
		request.setAttribute("mode", "add");
		request.setAttribute("dept", dept);
		return mapping.findForward("blank");		
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {		

		String dept = request.getParameter("dept");
		
		BCStkTransfer2BO bo = new BCStkTransfer2BO();						
		if (bo.add(form, request, response)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใบขอโอนเรียบร้อย");
			request.setAttribute("dept", dept);
			//request.setAttribute("pageForward", "<meta http-equiv='refresh' content='1;url=stktransfer2.do?mode=blank&dept="+dept+"'>");
			return mapping.findForward("success_add_stktransfer2");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("failure_add_stktransfer2");
		}	
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		BCStkTransfer2BO bo = new BCStkTransfer2BO();
		if(bo.view(form, request, response)) {
			//return mapping.findForward("blank");		
			return mapping.findForward("success_view_stktransfer2");	
		}else {
			return mapping.findForward("failure_view_stktransfer2");
		}				
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		BCStkTransfer2BO bo = new BCStkTransfer2BO();						
		if (bo.update(form, request, response)==true) {
			return mapping.findForward("success_update_stktransfer2");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("failure_update_stktransfer2");
		}		
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String dept = request.getParameter("dept").toString();		
		BCStkTransfer2BO bo = new BCStkTransfer2BO();						
		if (bo.delete(form, request, response)==true) {
			request.setAttribute("message", "ยกเลิกข้อมูลใบขอโอนเรียบร้อย");
			request.setAttribute("dept", dept);
			request.setAttribute("pageForward", "<meta http-equiv='refresh' content='1;url=stktransfer2.do?mode=showFind&dept="+dept+"'>");			
			return mapping.findForward("success_delete_stktransfer2");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("failure_delete_stktransfer2");
		}		
	}	
	
	public ActionForward showFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		new BCStkTransfer2BO().showFind(request);
		request.setAttribute("dept", request.getParameter("dept"));
		return mapping.findForward("success_stktransfer2_showFind");
	}
	
	@SuppressWarnings("static-access")
	public ActionForward popupItems(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
															
		new BCStkTransfer2BO().popupItems(request); 		
		return mapping.findForward("popupItems");
	}	
	
	@SuppressWarnings("unchecked")
	public ActionForward print(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String docNoParam = (String)request.getParameter("docNo");
		
		String hql = "from BCStkTransfer2 where docNo = '"+docNoParam+"'";
		BCStkTransfer2DAO dao = new BCStkTransfer2DAO();
		ArrayList<BCStkTransfer2> results = (ArrayList)dao.find(hql);
		
		String nameOfReport = "";
		
		if (results != null ) {
			BCStkTransfer2 s2 = (BCStkTransfer2)results.get(0);			
			
			if (s2.getBcStkTransfSubs().size()==1) {
				//System.out.println("=1");
				nameOfReport = "/ireport/rptStkTransfer2_1lineItem.jasper";
			}else {
				//System.out.println(">1");
				nameOfReport = "/ireport/rptStkTransfer2.jasper";
			}
		}
					
		HashMap hashMap = new HashMap();
		hashMap.put("docNoParam", docNoParam);
		
		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}
		
		return null;			
	}	
}
